package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Modelo.cd;

public class daoCd extends dao{

	
	public void inserirCd(cd cd) {

		String sql = "insert into musico (ehDubplo,titulo,ano) values(?,?,?)";

		try {
			stmt = con.prepareStatement(sql);

			stmt.setBoolean(1, cd.isEhDubplo());
			stmt.setString(2, cd.getTitulo());
			stmt.setInt(3,cd.getAno());
			
			stmt.execute();
			stmt.close();

		}

		catch (SQLException e) {
			throw new RuntimeException(e);

		}

	}

	public void atualizarCd(cd cd) {

		String sql = "update musica set ehDubplo =? ,titulo =?,ano =? where titulo=?";

		try {
			stmt = con.prepareStatement(sql);

			stmt.setBoolean(1, cd.isEhDubplo());
			stmt.setString(2, cd.getTitulo());
			stmt.setInt(3,cd.getAno());
			
			stmt.execute();
			stmt.close();

		}

		catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public ArrayList<cd> listarCd() {

		String sql = "select * from cd";

		try {
			stmt = con.prepareStatement(sql);

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {

				cd mu = new cd();

				mu.setEhDubplo(rs.getBoolean("ehDubplo"));
				mu.setTitulo(rs.getString("titulo"));
				mu.setAno(rs.getInt("ano"));
				cd.add(mu);
			}
			return listarCd();

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public ArrayList<cd> buscarCd(String campo,cd mu) {
		try {
			ArrayList<cd> mu1 = new ArrayList<>();
			String sql1 = "select * from cd where" + campo + "=?";

			stmt = con.prepareStatement(sql1);

			if (campo.equals("ehDubplo"))
				stmt.setBoolean(1, mu.isEhDubplo());
			else if (campo.equals(" titulo"))
				stmt.setString(1, mu.getTitulo());
			else if (campo.equals(" ano"))
				stmt.setInt(1, mu.getAno());

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {

				cd mu2 = new cd();

				mu.setEhDubplo(rs.getBoolean("ehDubplo"));
				mu.setTitulo(rs.getString("titulo"));
				mu.setAno(rs.getInt("ano"));
				
				cd.add(mu);

			}

			return mu1;

		} catch (SQLException e) {
			
			throw new RuntimeException(e);
		}
	}

	public void apagarCd(cd cd) {
		try {

			String sql1 = "delete from musica where ehDubplo =?";
			stmt = con.prepareStatement(sql1);
			stmt.setBoolean(1, cd.isEhDubplo());

			stmt.executeUpdate(sql1);
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}

